package com.baibei.ebec.web;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.webkit.WebView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.WebViewFragment;
import com.baibei.ebec.home.R;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicFragmentActivity;
import com.baibei.module.EasyJsBridge;
import com.blankj.utilcode.utils.LogUtils;

/**
 * 网页浏览
 * Created by ChenRui on 2017/8/3 0003 19:08.
 */
@Route(path = AppRouter.ROUTER_WEB)
public class WebViewActivity extends BasicFragmentActivity implements WebViewFragment.OnFragmentListener {

    private String mUrl;
    private String mHtmlForm;

    public void setUrl(String url) {
        mUrl = url;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected Fragment onCreateFragment() {
        mUrl = getIntent().getStringExtra(AppRouter.EXTRA_URL);
        mHtmlForm = getIntent().getStringExtra(AppRouter.EXTRA_FORM);

        //加载form
        if (!TextUtils.isEmpty(mHtmlForm)) {
            return WebViewFragment.newInstanceByHtml(mHtmlForm);
        }

        //加载url
        if (getIntent().getData() != null) {
            mUrl = getIntent().getData().toString();
            ((WebViewFragment) mFragment).loadUrl(mUrl);
        }
        return WebViewFragment.newInstance(mUrl);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("AddJavascriptInterface")
    @Override
    public void onReadyListener(Fragment fragment, WebView webView) {
        LogUtils.e("fragment:" + fragment);
        LogUtils.e("webView:" + webView);
        if (webView != null) {
            webView.addJavascriptInterface(new EasyJsBridge(this), "android");
            LogUtils.e("fragment_webview","here");
        }
    }

}
